// pages/singListDetail/singListDetail.js
const common = require("../../common/js/common.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    singList: "",
    stopIcon: "../../assets/stopsinglist.png",
    playIcon: "../../assets/playsinglist.png",
    httpTask: '',
    playIndex: "unKnow",
    playMin: true,
    flagCheck: false,
    songInfo: {
      musicSrc: "",
      songName: "",
      singerName: "",
      singerPic: "",
      flag: false
    }
  },
  onLoad: function (options) {
    console.log(options);
    let that = this;
    common.customLoading();
    common.http({
      url: common.baseUrl + "/playlist/detail",
      query: {
        id: options.id
      },
      success(data) {
        console.log(data);
        that.setData({
          singList: data
        });
      },
      fail(err) {
        common.coustomToast({
          title: err
        });
      },
      complete() {
        wx.hideLoading();
      }
    });
  },
  onReady: function () { },
  onShow: function () { },
  onHide: function () { },
  onUnload: function () { },
  onPullDownRefresh: function () { },
  onReachBottom: function () { },
  onShareAppMessage: function () { },
  audioOnStop() {
    let that = this;
    that.setData({
      playIndex: that.data.playIndex + "|pause",
      flagCheck: true
    });
  },
  audioOnPause() {
    let that = this;
    that.setData({
      playIndex: that.data.playIndex + "|pause"
    });
  },
  audioOnPlay() {
    let that = this;
    that.setData({
      playIndex: (that.data.playIndex + "").split("|")[0] - 0,
      playMin: false
    });
  },
  jumpSingPlayFun() {
    let that = this;
    wx.navigateTo({
      url:
        "../play/play?id=" +
        that.data.singList.playlist.tracks[(that.data.playIndex + "").split("|")[0] - 0].id,
      success(data) {
        console.log(data);
      },
      fail(error) {
        console.log(error);
      }
    });
  },
  prePlayMusic() {
    let that = this;
    changeMusic(that, (that.data.playIndex + "").split("|")[0] - 0 - 1, that.data.httpTask);
  },
  nextPlayMusic() {
    let that = this;
    changeMusic(that, (that.data.playIndex + "").split("|")[0] - 0 + 1, that.data.httpTask);
  },
  playMusic(e) {
    let that = this;
    let dataset = e.currentTarget.dataset;
    let httpTask = "";
    if (that.data.playIndex == dataset.index) {
      common.bgMusic.bgPause();
      that.setData({
        playIndex: that.data.playIndex + "|pause"
      });
      console.log(that.data.playIndex);
    } else {
      /*开始*/
      if (((that.data.playIndex + "").split("|")[0]) == dataset.index) {
        that.setData({
          playIndex: dataset.index,
          playMin: false
        });
        common.bgMusic.bgPlay();
        return;
      }
      changeMusic(that, dataset.index, that.data.httpTask);
      // that.setData({
      //   playIndex: dataset.index,
      //   playMin: false
      // });
      // if (httpTask != "") {
      //   httpTask.abort();
      // }
      // httpTask = common.http({
      //   url: common.baseUrl + "/music/url",
      //   query: {
      //     id: dataset.songid
      //   },
      //   success(data) {
      //     that.setData({
      //       songInfo: {
      //         musicSrc: data.data[0].url,
      //         songName: formatSingerName(singList.ar),
      //         singerName: singList.al.name,
      //         singerPic: singList.al.picUrl,
      //         flag: that.data.flagCheck
      //       }
      //     });
      //     that.data.flagCheck ? that.setData({ flagCheck: false }) : null;
      //   },
      //   fail(err) {
      //     common.coustomToast({
      //       title: err
      //     });
      //   }
      // });
    }
  }
});
function formatSingerName(array) {
  let name = [];
  array.forEach(n => {
    name.push(n.name);
  });
  return name;
}
function changeMusic(that, index, httpTask) {
  let singList = that.data.singList.playlist.tracks[index];
  let httpRquest;
  let trackIds = that.data.singList.playlist.trackIds;
  if (index < 0 || trackIds.length - 1 < index) {
    common.coustomToast({
      title: "没有更多Music"
    });
    return;
  }
  that.setData({
    playIndex: index,
    playMin: false
  });
  if (httpTask != "") {
    httpTask.abort();
  }
  console.log(index);
  console.log(trackIds[index].id);
  httpRquest = common.http({
    url: common.baseUrl + "/music/url",
    query: {
      id: trackIds[index].id
    },
    success(data) {
      that.setData({
        songInfo: {
          musicSrc: data.data[0].url,
          songName: formatSingerName(singList.ar),
          singerName: singList.al.name,
          singerPic: singList.al.picUrl,
          flag: that.data.flagCheck
        }
      });
      that.data.flagCheck ? that.setData({ flagCheck: false }) : null;
    },
    fail(err) {
      common.coustomToast({
        title: err
      });
    }
  });
  that.setData({
    httpTask: httpRquest
  });
}
